package com.lemon.tankgame3;

import java.io.*;
import java.util.Vector;

/**
 * @author 软柠柠吖
 * @date 2022/2/23
 * 该类用于记录相关的信息
 * 和文件交互
 */
public class Recorder {
    // 定义变量，记录我方击毁敌人坦克数
    private static int hitTankNum = 0;
    // 定义IO对象，准备写数据到文件
    private static BufferedWriter bufferedWriter = null;
    private static BufferedReader bufferedReader = null;
    // 把记录文件保存到 src下
    // private static String recordFilePath = "D:\\myRecord.txt";
    private static String recordFilePath = "src\\myRecord.txt";

    // 定义一个Node 的Vector，用于保存坦克的信息
    private static Vector<Node> nodes = new Vector<>();

    // 返回记录文件的目录
    public static String getRecordFilePath() {
        return recordFilePath;
    }

    // 增加一个方法，用于读取recordFilePath 文件，恢复相关信息
    // 该方法，在继续上局游戏的时候调用
    public static Vector<Node> getNodesAndHitTankNum() {
        try {
            bufferedReader = new BufferedReader(new FileReader(recordFilePath));
            hitTankNum = Integer.parseInt(bufferedReader.readLine());
            // 循环读取文件，生成Node集合
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                String[] xyd = line.split(" ");
                Node node = new Node(Integer.parseInt(xyd[0]), Integer.parseInt(xyd[1]), Integer.parseInt(xyd[2]));
                nodes.add(node);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return nodes;
    }

    // 增加一个方法，当游戏退出时，我们将 hitTankNum 保存到 "D:\\myRecord.txt" 文件中
    // 对keepRecord 进行升级，保存敌人坦克的坐标和方向
    public static void keepRecord() {
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(recordFilePath));
            bufferedWriter.write(hitTankNum + "");
            bufferedWriter.newLine();
            // 遍历敌人坦克的Vector， 根据情况保存即可
            for (int i = 0; i < Enemy.enemies.size(); i++) {
                Enemy enemy = Enemy.enemies.get(i);
                // 建议判断
                if (enemy.isLive()) {
                    // 保存该enemy 坦克信息
                    String record = enemy.getX() + " " + enemy.getY() + " " + enemy.getDirect();
                    // 写入到文件
                    bufferedWriter.write(record);
                    bufferedWriter.newLine();
                }
            }

            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //

    public static int getHitTankNum() {
        return hitTankNum;
    }

    public static void setHitTankNum(int hitTankNum) {
        Recorder.hitTankNum = hitTankNum;
    }

    // 当我方坦克击毁一个敌人坦克，就应当 hitTankNum++
    public static void addHitTankNum() {
        hitTankNum++;
    }
}
